package lzyc.pointsManagement.business.userdept.service.impl;

/**
 * 
 * Description: date: 2024/3/17 12:30
 * 
 * @author yangyang
 */

import java.util.ArrayList;
import java.util.List;

import lzyc.pointsManagement.common.enums.UserStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import lzyc.pointsManagement.business.config.DeptGroupEnum;
import lzyc.pointsManagement.business.config.RoleEnum;
import lzyc.pointsManagement.business.userdept.domain.SysUserDept;
import lzyc.pointsManagement.business.userdept.service.SysUserDeptService;
import lzyc.pointsManagement.common.core.domain.entity.SysDept;
import lzyc.pointsManagement.common.core.domain.entity.SysGroup;
import lzyc.pointsManagement.common.core.domain.entity.SysUser;
import lzyc.pointsManagement.common.utils.DateUtils;
import lzyc.pointsManagement.common.utils.ShiroUtils;
import lzyc.pointsManagement.common.utils.StringUtils;
import lzyc.pointsManagement.framework.shiro.service.SysPasswordService;
import lzyc.pointsManagement.system.service.ISysDeptService;
import lzyc.pointsManagement.system.service.ISysRoleService;
import lzyc.pointsManagement.system.service.ISysUserService;

/**
 * SysUserDeptServiceImpl 用户部门基本信息
 * 
 * @author 杨某人
 * @version 2024/03/17 12:30
 **/
@Component
public class SysUserDeptServiceImpl implements SysUserDeptService {

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ISysRoleService roleService;

    @Autowired
    private ISysDeptService deptService;

    @Autowired
    private SysPasswordService passwordService;

    public static final int ERROR_CODE = 0;

    /**
     * 分页获取用户列表
     * 
     * @param user
     * @return
     */
    @Override
    public List<SysUserDept> selectUserList(SysUser user) {
        if (!ShiroUtils.isAdmin()){
            user.setUserId(ShiroUtils.getUserId());
        }
        List<SysUserDept> userDeptlist=new ArrayList<>();
        List<SysUser> sysUserList = userService.selectUserList(user);
        for (SysUser sysUser : sysUserList) {
            userDeptlist.add(toSysUserDept(sysUser));
        }
        return userDeptlist;
    }

    /**
     * 新增
     *
     * @param userDept
     * @return
     */
    @Override
    public int addSave(SysUserDept userDept) {
        SysUser user = getUserByUserDept(userDept);
        if (StringUtils.isEmpty(userDept.getPassword())){
            return ERROR_CODE;
        }
        if (StringUtils.isEmpty(userDept.getSalt())){
            userDept.setSalt(ShiroUtils.randomSalt());
        }
        user.setSalt(userDept.getSalt());
        user.setPassword(passwordService.encryptPassword(userDept.getLoginName(), userDept.getPassword(), userDept.getSalt()));
        user.setPwdUpdateDate(DateUtils.getNowDate());
        user.setCreateBy(ShiroUtils.getLoginName());
        if (userDept.getDeptId() == null&&StringUtils.isNotEmpty(userDept.getDeptName())) {
            SysDept dept = getDeptByUserDept(userDept);
            int isSaveDept = deptService.insertDept(dept);
            if (isSaveDept > 0) {
                SysDept sysDept = deptService.selectDeptByName(userDept.getDeptName());
                user.setDeptId(sysDept.getDeptId());
            } else {
                return ERROR_CODE;
            }
        }
        if (userDept.getGroupID() == null&&StringUtils.isNotEmpty(userDept.getGroupName())) {
            SysDept group = getGroupByUserDept(userDept);
            int isSaveGroup = deptService.insertDept(group);
            if (isSaveGroup > 0) {
                SysDept sysGroup = deptService.selectDeptByName(userDept.getGroupName());
                user.setGroupId(sysGroup.getDeptId());
            } else {
                return ERROR_CODE;
            }
        }
        int isSaveUser = userService.insertUser(user);

        if (isSaveUser > 0) {
            SysUser sysUser = userService.selectUserByLoginName(user.getLoginName());
            return roleService.insertAuthUsers(sysUser.getUserId(), RoleEnum.COMMON_USER.getValue());
        } else {
            return ERROR_CODE;
        }
    }

    /**
     * 修改
     *
     * @param userDept
     * @return
     */
    @Override
    public int editSave(SysUserDept userDept) {
        SysUser user = userService.selectUserById(userDept.getUserId());
        user.setDeptId(userDept.getDeptId());
        user.setGroupId(userDept.getGroupID());
        user.setUserName(userDept.getUserName());
        user.setPhonenumber(userDept.getPhonenumber());
        user.setAvatar(userDept.getAvatar());
        user.setUpdateBy(ShiroUtils.getLoginName());
        if (StringUtils.isNotEmpty(userDept.getPassword())) {
            user.setSalt(ShiroUtils.randomSalt());
            user.setPassword(passwordService.encryptPassword(user.getLoginName(), userDept.getPassword(), user.getSalt()));
            user.setPwdUpdateDate(DateUtils.getNowDate());
            user.setStatus(UserStatus.OK.getCode());
        }
        if (StringUtils.isNull(userDept.getDeptId())&&StringUtils.isNotEmpty(userDept.getDeptName())) {
            int isSaveDept = deptService.insertDept(getDeptByUserDept(userDept));
            if (isSaveDept > 0) {
                SysDept dept = deptService.selectDeptByName(userDept.getDeptName());
                user.setDeptId(dept.getDeptId());
            } else {
                return ERROR_CODE;
            }
        }
        if (StringUtils.isNull(userDept.getGroupID())&&StringUtils.isNotEmpty(userDept.getGroupName())) {
            int isSaveGroup = deptService.insertDept(getGroupByUserDept(userDept));
            if (isSaveGroup > 0) {
                SysDept group = deptService.selectDeptByName(userDept.getGroupName());
                user.setGroupId(group.getDeptId());
            } else {
                return ERROR_CODE;
            }
        }
        return userService.updateUser(user);
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @Override
    public int deleteUserByIds(String ids) {
        return userService.deleteUserByIds(ids);
    }

    /**
     * 通过ID查询
     *
     * @param userId
     * @return
     */
    @Override
    public SysUserDept selectUserById(Long userId) {
        return toSysUserDept(userService.selectUserById(userId));
    }

    /**
     * 校验用户名是否唯一
     *
     * @param user
     * @return
     */
    @Override
    public boolean checkLoginNameUnique(SysUserDept user) {
        return userService.checkLoginNameUnique(toSysUser(user));
    }

    /**
     * 获取已读用户列表
     *
     * @param projectID 事项ID
     * @return
     */
    @Override
    public List<SysUserDept> userRead(String projectID) {
        List<SysUserDept> userDeptlist=new ArrayList<>();
        List<SysUser> sysUserList = userService.selectReadUserList(projectID);
        for (SysUser sysUser : sysUserList) {
            userDeptlist.add(toSysUserDept(sysUser));
        }
        return userDeptlist;
    }

    /**
     * 获取未读用户列表
     *
     * @param projectID 事项ID
     * @return
     */
    @Override
    public List<SysUserDept> userUnread(String projectID) {
        List<SysUserDept> userDeptlist=new ArrayList<>();
        List<SysUser> sysUserList = userService.selectUnreadUserList(projectID);
        for (SysUser sysUser : sysUserList) {
            userDeptlist.add(toSysUserDept(sysUser));
        }
        return userDeptlist;
    }

    /**
     * 转SysUserDept
     * @param sysUser
     * @return
     */
    public SysUserDept toSysUserDept(SysUser sysUser) {
        SysUserDept sysUserDept=new SysUserDept();
        sysUserDept.setUserId(sysUser.getUserId());
        sysUserDept.setLoginName(sysUser.getLoginName());
        sysUserDept.setUserName(sysUser.getUserName());
        sysUserDept.setPhonenumber(sysUser.getPhonenumber());
        sysUserDept.setAvatar(sysUser.getAvatar());
        sysUserDept.setPassword(sysUser.getPassword());
        sysUserDept.setSalt(sysUser.getSalt());
        sysUserDept.setStatus(sysUser.getStatus());
        sysUserDept.setDelFlag(sysUser.getDelFlag());
        if (StringUtils.isNotNull(sysUser.getDeptId())){
            sysUserDept.setDeptId(sysUser.getDeptId());
            sysUserDept.setDeptName(sysUser.getDept().getDeptName());
        }
        if (StringUtils.isNotNull(sysUser.getGroupId())){
            sysUserDept.setGroupID(sysUser.getGroupId());
            sysUserDept.setGroupName(sysUser.getGroup().getGroupName());
        }
        return sysUserDept;
    }

    /**
     * 转SysUser
     * @param sysUserDept
     * @return
     */
    public SysUser toSysUser(SysUserDept sysUserDept) {
        SysUser sysUser=new SysUser();
        sysUser.setUserId(sysUserDept.getUserId());
        sysUser.setLoginName(sysUserDept.getLoginName());
        sysUser.setUserName(sysUserDept.getUserName());
        sysUser.setPhonenumber(sysUserDept.getPhonenumber());
        sysUser.setAvatar(sysUserDept.getAvatar());
        sysUser.setPassword(sysUserDept.getPassword());
        sysUser.setSalt(sysUserDept.getSalt());
        sysUser.setStatus(sysUserDept.getStatus());
        sysUser.setDelFlag(sysUserDept.getDelFlag());
        if (StringUtils.isNotNull(sysUserDept.getDeptId())){
            sysUser.setDeptId(sysUserDept.getDeptId());
            SysDept dept=new SysDept();
            dept.setDeptId(sysUserDept.getDeptId());
            dept.setDeptName(sysUserDept.getDeptName());
            sysUser.setDept(dept);
        }
        if (StringUtils.isNotNull(sysUserDept.getGroupID())){
            sysUser.setGroupId(sysUserDept.getGroupID());
            SysGroup group=new SysGroup();
            group.setGroupId(sysUserDept.getGroupID());
            group.setGroupName(sysUserDept.getGroupName());
            sysUser.setGroup(group);
        }
        return sysUser;
    }

    /**
     * 通过loginName查询
     *
     * @param loginName
     * @return
     */
    @Override
    public SysUserDept selectUserByLoginName(String loginName) {
        return toSysUserDept(userService.selectUserByLoginName(loginName));
    }

    /**
     * @param dept
     * @return
     */
    @Override
    public List<SysDept> deptList(SysDept dept) {
        dept.setLeader(DeptGroupEnum.DEPT_TYPE.getValue());
        return deptService.selectDeptList(dept);
    }

    /**
     * @param group
     * @return
     */
    @Override
    public List<SysDept> groupList(SysDept group) {
        group.setLeader(DeptGroupEnum.GROUP_TYPE.getValue());
        return deptService.selectDeptList(group);
    }

    public SysUser getUserByUserDept(SysUserDept userDept) {
        SysUser sysUser = new SysUser();
        sysUser.setUserId(userDept.getUserId());
        sysUser.setDeptId(userDept.getDeptId());
        sysUser.setGroupId(userDept.getGroupID());
        sysUser.setLoginName(userDept.getLoginName());
        sysUser.setUserName(userDept.getUserName());
        sysUser.setPhonenumber(userDept.getPhonenumber());
        sysUser.setAvatar(userDept.getAvatar());
        sysUser.setPassword(userDept.getPassword());
        sysUser.setStatus(userDept.getStatus());
        return sysUser;
    }

    public SysDept getDeptByUserDept(SysUserDept userDept) {
        SysDept dept = new SysDept();
        dept.setDeptId(userDept.getDeptId());
        dept.setDeptName(userDept.getDeptName());
        dept.setLeader(DeptGroupEnum.DEPT_TYPE.getValue());
        return dept;
    }

    public SysDept getGroupByUserDept(SysUserDept userDept) {
        SysDept dept = new SysDept();
        dept.setDeptId(userDept.getGroupID());
        dept.setDeptName(userDept.getGroupName());
        dept.setLeader(DeptGroupEnum.GROUP_TYPE.getValue());
        return dept;
    }
}
